/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package work.base.innerclass;

import java.util.*;

/**
 * @author Sakura Shen
 * @description 匿名类
 * @date 2021/7/26
 */
public class AnonymousClass {

    public void setAnonymous() {
        // 继承一个父类实现匿名内部类
        work.base.innerclass.Person p = new Person() {
            public void eat() {
                System.out.println("eat something");
            }
        };
        p.eat();

        // 在接口上使用匿名内部类
        // 这里，不是实例化接口，而是实例化一个实现接口的、未命名的类
        Animal animal = () -> System.out.println("interface Animal eat()");
        animal.eat();

    }

    /**
     * ArrayList的匿名内部类写法
     */
    public void setListAnonymous() {

        ArrayList<String> list = new ArrayList<String>() {{
            add("A");
            add("B");
            add("C");
        }};

        Map map = new HashMap() {{
            put("Name", "Unmi");
            put("QQ", "1125535");
        }};

    }

    public static void main(String[] args) {
        // 实现了List接口的匿名类（匿名内部类）
        List a  = new List<String>(){

            @Override
            public int size() {
                return 0;
            }

            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public boolean contains(Object o) {
                return false;
            }

            @Override
            public Iterator<String> iterator() {
                return null;
            }

            @Override
            public Object[] toArray() {
                return new Object[0];
            }

            @Override
            public <T> T[] toArray(T[] ts) {
                return null;
            }

            @Override
            public boolean add(String s) {
                return false;
            }

            @Override
            public boolean remove(Object o) {
                return false;
            }

            @Override
            public boolean containsAll(Collection<?> collection) {
                return false;
            }

            @Override
            public boolean addAll(Collection<? extends String> collection) {
                return false;
            }

            @Override
            public boolean addAll(int i, Collection<? extends String> collection) {
                return false;
            }

            @Override
            public boolean removeAll(Collection<?> collection) {
                return false;
            }

            @Override
            public boolean retainAll(Collection<?> collection) {
                return false;
            }

            @Override
            public void clear() {

            }

            @Override
            public String get(int i) {
                return null;
            }

            @Override
            public String set(int i, String s) {
                return null;
            }

            @Override
            public void add(int i, String s) {

            }

            @Override
            public String remove(int i) {
                return null;
            }

            @Override
            public int indexOf(Object o) {
                return 0;
            }

            @Override
            public int lastIndexOf(Object o) {
                return 0;
            }

            @Override
            public ListIterator<String> listIterator() {
                return null;
            }

            @Override
            public ListIterator<String> listIterator(int i) {
                return null;
            }

            @Override
            public List<String> subList(int i, int i1) {
                return null;
            }
        };


    }
}
